<?php
namespace XtwsBack\Controller;
use Think\Controller;

class GroupCopyController extends CommonController {

    //首页
    public function index(){
        $copy = M('shop_spellgroup_copy');
        $count = $copy->where(array('num'=>array('neq',0)))->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $copy->table(C('DB_PREFIX')."shop_spellgroup_copy as c")
                ->join(C('DB_PREFIX')."shop_spellgroup as g ON g.id = c.sg_id")
                ->field('c.*,g.u_id,g.s_id,g.type,g.title,g.h_imgs,g.actual_price,min_num,endtime')
                ->where(array('c.num'=>array('neq',0)))
                ->order('id desc')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->select();
        foreach($list as $k => $v) {// 查询拼团商品信息，拼团的商家或者农户的信息
            $list[$k]['h_imgs'] = isMorePic($list[$k]['h_imgs']);
            // 查询商家或农户的姓名与头像
            $list[$k]['m_info'] = getMember($list[$k]['u_id'], 'nickname,avatar');
            if ($list[$k]['type'] == 1) {//社区拼团-查询店铺名称
                $list[$k]['m_info']['storename'] = getStore($list[$k]['s_id']);
            } else {//乡村拼团-查询农户所在的村庄
                $list[$k]['m_info']['storename'] = M('member_farmers')->where('u_id = ' . $list[$k]['u_id'])->getField('village');
            }// end if
        }//end foreach

//        dump($list);
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        //加载视图
        $this->display();
    }//end fun


    // 订单详情
    public function copyinfo(){
        $id = intval(I('id'));
        $map['id'] = $id;
        $info = getInfo('shop_spellgroup_copy',$map);
        if($info){
            // 根据sg_id查询拼团商品信息
            $goods_info = getInfo('shop_spellgroup',array('id'=>$info['sg_id']),'u_id,s_id,type,title,endtime,h_imgs,actual_price,min_num');
            $goods_info['h_imgs'] = explode(",",$goods_info['h_imgs']);
            $goods_info['endtime'] = $goods_info['endtime']-time();    //处理拼团失效时间
            // 查询店铺老板或者农户的头像、昵称
            $m_info = getMember($goods_info['u_id'],'nickname,avatar');
            if($goods_info['type']==1){// 社区拼团-查询店铺名称
                $m_info['storename'] = getStore($goods_info['s_id']);
            }else{// 乡村拼团查询农户乡村名称
                $m_info['storename'] = M('member_farmers')->where('u_id = '.$goods_info['u_id'])->getField('village');
            }// end if

            // 获取所有副本的用户信息
            if($info['num']!=0){
                // 该拼团所有的订单信息
                $where['state'] = array('neq',0);
                $where['c_id'] = $info['id'];
                $allorder= M('shop_spellgroup_order')->where($where)->field('u_id')->select();
                $a = M('shop_spellgroup_order')->getLastSql();
                // 订单信息中所有的用户信息
                foreach($allorder as $k => $v){
                    $group_member[$k] = getMember($v['u_id'],'nickname,avatar');
                }// end foreach
                $b = M('member')->getLastSql();
                $this -> assign('group_member',$group_member);
            }// end if
            // 获取订单地址
            $addr_info = M('shop_community')->where(array('id' => $info['c_id'] ))->field('title,a_id_1,a_id_2,a_id_3')->find();
            $addr_info['pro'] = M('area')->where('id='.$addr_info['a_id_1'])->getField('name');
            $addr_info['city'] = M('area')->where('id='.$addr_info['a_id_2'])->getField('name');
            $addr_info['area'] = M('area')->where('id='.$addr_info['a_id_3'])->getField('name');

            $this -> assign('info',$info);              // 订单信息
            $this -> assign('goods_info',$goods_info);  // 拼团商品信息
            $this -> assign('m_info',$m_info);  // 店铺老板信息
            $this -> assign('addr_info',$addr_info);    // 地址
            $this->display();
        }else{
            $this->error('没有记录');
        }// end if

    }// end fun orderinfo()




}//end class